package com.wzdigit.wms.wms.task;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.wms.basic.domain.mid.dtos.IfCompDeliveryHeaderDto;
import com.wzdigit.wms.wms.constant.SyncTypeConstant;
import com.wzdigit.wms.wms.exception.SyncException;
import com.wzdigit.wms.wms.service.mid.IfCompDeliveryService;
import com.wzdigit.wms.wms.service.mid.IfTransferHeaderService;
import com.wzdigit.wms.wms.service.mid.SyncErrorDataService;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName IfCompDeliveryTask
 * @Description 接口中间表-公司间交货，根据业务类型同步到对应表
 * @Author LHR-PC
 * @Date 2021/3/17 9:58
 * @Version
 **/

@ConditionalOnExpression("${schedule.common.switch}")
@Component
@Slf4j
public class IfCompDeliveryTask {

    @Autowired
    private IfCompDeliveryService ifCompDeliveryService;

    @Autowired
    private SyncErrorDataService syncErrorDataService;

    @Autowired
    IfTransferHeaderService ifTransferHeaderService;

    final String syncType = SyncTypeConstant.IF_COMP_DELIVERY_HANDLE;


    @Scheduled(cron = "0 0/11 * * * ?")
    public synchronized void syncData() {
        try {
            log.info("接口中间表-公司间交货同步————start");
            long start = System.currentTimeMillis();

            List<IfCompDeliveryHeaderDto> datas = ifCompDeliveryService.selectSyncData();
            if (!CollectionUtil.isEmpty(datas)) {
                syncData(datas, false);
            }

            log.info("------接口中间表-公司间交货同步 => ? end:{}------", System.currentTimeMillis() - start);
        } catch (RestStatusException ex) {
            ex.printStackTrace();
        }
    }



    /**
     *
     * @param datas
     * @param isSyncError:false 正常数据 true 异常数据
     */
    public  void syncData(List<IfCompDeliveryHeaderDto> datas, boolean isSyncError) {
        for (IfCompDeliveryHeaderDto data : datas) {
            try {

                ifCompDeliveryService.syncData(data);
            } catch (Exception e) {
                log.warn("接口中间表-公司间交货同步失败：单号{}",data.getBillNo());
                log.warn("接口中间表-公司间交货同步失败：" + (e.getMessage() == null ? e.toString() : e.getMessage()));
                String message = "";
                if (SyncException.class.isInstance(e)) {
                    SyncException ex = (SyncException) e;
                    message = ex.getFailedReason();
                    if (message.length() >= 1000) {
                        message = message.substring(0, 500);
                    }
                    log.info("【接口中间表-公司间交货同步失败】" + ex.getFailedReason());
                    syncErrorDataService.saveSyncErrorData(ex.getSyncErrorType(), ex.getCode(),message);
                } else {
                    e.printStackTrace();
                    if (e.getMessage() == null) {
                        message = e.getClass().getName();
                    } else {
                        message = e.getMessage();
                    }
                    if (message.length() >= 1000) {
                        message = message.substring(0, 500);
                    }
                    syncErrorDataService.saveSyncErrorData(syncType, data.getBillNo(), message);
                }
                e.printStackTrace();
            }
        }
    }
}
